<?php
/**
 * Created by LíJimpéy(：
 * User: jimpey88
 * Date: 2024/3/28
 * Email: < jimpey@qq.com >
 * Project: company-admin-tp6
 */

namespace app\backend_api\model;

class AdminUsers extends BaseModel
{
    protected $hidden = ['password','deleted_at'];
    protected $append = ['role_id'];
    public function roles(): \think\model\relation\BelongsToMany
    {
        return $this->belongsToMany(AdminRoles::class, AdminWithRoles::class, 'role_id', 'admin_id')
            ->where('status',1);
        // todo 加上where查询 关联预加载 with() 报错
    }

    public function allRoles(): \think\model\relation\BelongsToMany
    {
        return $this->belongsToMany(AdminRoles::class, AdminWithRoles::class, 'role_id', 'admin_id');
    }

    /**
     * role_id 获取器
     * @return array
     */
    public function getRoleIdAttr()
    {
        return $this->roles()->column('role_id');
    }

}